<template>
  <div class="system-menu-container">
    <el-card shadow="hover">
      <div class="system-menu-search mb15">
        <el-form :inline="true">
          <el-form-item label="关键字">
            <el-input
                v-model="queryParams.name"
                placeholder="请输入关键字"
                clearable
                class="w-50 m-2"
                size="default"
                :input-style="{height: '30px'}"
            />
          </el-form-item>
          <el-form-item>
            <el-button size="default" type="primary" class="ml10" @click="handleQuery">
              <el-icon>
                <ele-Search/>
              </el-icon>
              查询
            </el-button>
            <el-button size="default" type="success" class="ml10" @click="onOpenAddMenu(null)"
                       v-auth="'api/v1/system/menu/add'">
              <el-icon>
                <ele-Plus/>
              </el-icon>
              新增科室
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table
          :data="departmentData"
          style="width: 100%"
          row-key="path"
          class="el-table-cus-height"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
        <el-table-column label="科室名" show-overflow-tooltip>
          <template #default="scope">
            <span class="ml10">{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="code" label="科室编码" show-overflow-tooltip></el-table-column>
        <el-table-column prop="chargePerson" label="负责人" show-overflow-tooltip></el-table-column>
        <el-table-column prop="tell" label="联系电话" show-overflow-tooltip></el-table-column>

        <el-table-column label="上班时间" width="180">
          <template #default="scope">
            <span class="ml10">{{ scope.row.startTime }}-{{ scope.row.endTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="240">
          <template #default="scope">
            <el-button size="small" text type="primary" @click="onOpenAddMenu(scope.row)">新增</el-button>
            <el-button size="small" text type="primary" @click="onOpenEditMenu(scope.row)">修改</el-button>
            <el-button size="small" text type="primary" @click="onCopyMenuInfo(scope.row)">复制</el-button>
            <el-button size="small" text type="danger" @click="onTabelRowDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <EditMenu ref="editMenuRef" @departmentList="departmentList" :visibleOptions="sys_show_hide" :acType="acType"/>
  </div>
</template>

<script lang="ts">
import {ref, toRefs, reactive, onBeforeMount, defineComponent, getCurrentInstance, unref} from 'vue';
import {ElMessageBox, ElMessage} from 'element-plus';
import EditMenu from '/@/views/system/menu/component/editMenu.vue';
import {getDepartments} from "/@/api/lib/department";
import {ElementPlus} from "@element-plus/icons-vue";

export default defineComponent({
  name: 'apiV1LibDepartments',
  components: {ElementPlus, EditMenu},
  setup() {
    const editMenuRef = ref();
    const state = reactive({
      queryParams: {
        name: "",
      },
      departmentData: [],
    });
    const {proxy} = getCurrentInstance() as any;
    const {sys_show_hide} = proxy.useDict('sys_show_hide')
    const acType = ref('add')
    // 打开新增菜单弹窗
    const onOpenAddMenu = (row: any) => {
      // acType.value = 'add'
      // editMenuRef.value.openDialog(row);
    };
    // 打开编辑菜单弹窗
    const onOpenEditMenu = (row: any) => {
      // acType.value = 'edit'
      // editMenuRef.value.openDialog(row);
    };
    // 打开编辑菜单弹窗
    const onCopyMenuInfo = (row: any) => {
      // acType.value = 'add'
      // editMenuRef.value.openDialog(row);
    };
    // 删除当前行
    const onTabelRowDel = (row: any) => {
      ElMessageBox.confirm(`此操作将永久删除菜单：“${row.title}”, 是否继续?`, '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
          .then(() => {
            // delMenu(row.id).then(() => {
            //   ElMessage.success('删除成功');
            //   proxy.$refs['editMenuRef'].resetMenuSession()
            //   departmentList();
            // })
          })
          .catch(() => {
          });
    };
    const formatIsHide = (row: any) => {
      return proxy.selectDictLabel(unref(sys_show_hide), '' + row.isHide);
    };
    onBeforeMount(() => {
      departmentList()
    });
    const handleQuery = () => {
      departmentList();
    };
    const departmentList = () => {
      getDepartments(state.queryParams).then(res => {
        state.departmentData = proxy.handleTree(res.data ?? [], "id", "pid");
        console.log(state.departmentData)
      })
    };
    return {
      editMenuRef,
      onOpenAddMenu,
      onOpenEditMenu,
      onCopyMenuInfo,
      onTabelRowDel,
      formatIsHide,
      departmentList,
      handleQuery,
      ...toRefs(state),
      sys_show_hide,
      acType
    };
  },
});
</script>
